const db = require('../config/db')

// 可视化数据统计
class Visual {
     // 获取所有文章数量
     total_article() {
        return new Promise((resolve, reject) => {
            // 统计db_message这个表格的内容数量
            let sql = `select count(*) as total from db_article`
            db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: `统计文章数量成功。`,
                            result:result[0]
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '统计数据失败。'
                        })
                    }
                )
        })
    }

    // 获取所有用户数量
    total_user() {
        return new Promise((resolve, reject) => {
            let sql = `select count(*) as total from db_user`
            db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: `统计文章数量成功。`,
                            result:result[0]
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '统计数据失败。'
                        })
                    }
                )
        })
    }

    // 获取所有宠物信息
    // 统计领养宠物记录分类总数量
    total_pet(petcategory) {
        return new Promise((resolve, reject) => {
            let sql = `select count(*) as total from db_pet where petcategory = '${petcategory}'`;
            db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: "统计数据成功。",
                            category: petcategory,
                            total: result[0].total,
                            result:result[0]
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "统计信息失败",
                            err
                        })
                    }
                )
        })
    }

    // 获取导航列表
    classify_list() {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_classify`;
            db.query(sql).then(
                (rows) => {
                    resolve({
                        code: 200,
                        msg: '获取导航列表成功。',
                        result: rows
                    })
                },
                (err) => {
                    reject({
                        code: -1,
                        msg: '获取导航列表失败。',
                        err
                    })
                }
            )
        })
    }

    // 获取分类对应的数据
    async  catetory_data(){
        let arr = [];
        let newArr = [];
        // 首先获取所有分类
        await this.classify_list()
        .then(
            content=>{
                let {code, result} = content;
                if(code == 200) {
                    arr = result.map(item=>{
                        return item.classify_text
                    })
                }
            },
            err=>{
                console.log(err)
            }
        )
        for(let i = 0 ; i < arr.length ; i ++){
            await this.total_pet(arr[i])
            .then(
                content=>{
                     let {code , category, total} = content;
                     if(code == 200) {
                        newArr.push({
                            category,
                            total,
                            id: i 
                        })
                     }
                }
            )
        }

        // 筛选数据
        return new Promise((resolve,reject)=>{
             if(newArr.length!=0){
                resolve({
                    code: 200,
                    result: newArr,
                    msg: "统计分类宠物数量。"
                })
             }
        })

    }

    // 所有发布的领养信息数量
    pet_num() {
        return new Promise((resolve, reject) => {
            // 统计db_message这个表格的内容数量
            let sql = `select * from db_pet`
            db.query(sql)
                .then(
                    result => {
                        // 所有发布的宠物信息
                        resolve(result)
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '获取领养信息失败。'
                        })
                    }
                )
        })
    }

    // 统计领养宠物记录总数量
    total_pet_all() {
        return new Promise((resolve, reject) => {
            let sql = `select count(*) as total from db_pet`;
            db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: "统计数据成功。",
                            total: result[0].total,
                            result:result[0]
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "统计信息失败",
                            err
                        })
                    }
                )
        })
    }

    // 获取所有收藏数量
    total_collect() {
        return new Promise((resolve, reject) => {
            // 统计db_message这个表格的内容数量
            let sql = `select count(*) as total from db_collect`
            db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: `统计收藏数量成功。`,
                            result:result[0]
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '统计数据失败。'
                        })
                    }
                )
        })
    }
}

module.exports = new Visual();